package com.bw.login.view

import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.bw.base.constants.ApiConst
import com.bw.base.constants.RoutePath
import com.bw.base.constants.UserConst
import com.bw.common.view.BaseMvvmActivity
import com.bw.login.databinding.ActivityLogin2Binding
import com.bw.login.databinding.ActivityLoginBinding
import com.bw.login.net.LoginViewModel
import com.tencent.mmkv.MMKV

/**
 * 自定义注解的注解叫做元注解
 */

@Route(path = RoutePath.PATH_LOGIN)
class LoginActivity : BaseMvvmActivity<ActivityLogin2Binding, LoginViewModel>() {
    override fun initView() {


        binding.username.setText("lhy")
        binding.password.setText("123456")



        binding.login.setOnClickListener {
            val username = binding.username.text.toString()
            val password = binding.password.text.toString()


            if(username.equals("")||password.equals("")){
                ToastUtils.showShort("请先输入账号和密码")
                return@setOnClickListener
            }

            vm.login(username, password)
        }

        binding.register.setOnClickListener {
            ARouter.getInstance().build(RoutePath.PATH_REGISTER).navigation()
        }



    }

    override fun initData() {
        vm.loginSuccess.observe(this) {
            ToastUtils.showLong("登录成功")
            MMKV.defaultMMKV().encode(UserConst.USER_ID,it.id)
            MMKV.defaultMMKV().encode(UserConst.USER_NAME,it.username)
            MMKV.defaultMMKV().encode(ApiConst.PARAM_TOKEN,it.token)
            ARouter.getInstance().build(RoutePath.PATH_MAIN).navigation()
            finish()
        }
    }



}